export default class Logger {
  constructor(private readonly loggerName: string) {
  }

  public error(message?: any, ...optionalParams: any[]): void {
    console.error(this.formatMessage('error', message), ...optionalParams);
  }

  public info(message?: any, ...optionalParams: any[]): void {
    console.info(this.formatMessage('info', message), ...optionalParams);
  }

  public warn(message?: any, ...optionalParams: any[]): void {
    console.warn(this.formatMessage('warn', message), ...optionalParams);
  }

  public debug(message?: any, ...optionalParams: any[]): void {
    console.debug(this.formatMessage('debug', message), ...optionalParams);
  }

  public trace(message?: any, ...optionalParams: any[]): void {
    console.trace(this.formatMessage('trace', message), ...optionalParams);
  }

  private formatMessage(type: string, message?: any): string {
    const time = new Date();
    const minutes = time.getMinutes();
    let minutesString = '';
    if (minutes >= 100) {
      minutesString = '' + minutes;
    } else if (minutes >= 10) {
      minutesString = '0' + minutes;
    } else {
      minutesString = '00' + minutes;
    }

    const dateString = `${time.getFullYear()}/${Logger.formatNumber(time.getMonth() + 1)}/${Logger.formatNumber(time.getDay())}`;
    const timeString = `${Logger.formatNumber(time.getHours())}:${Logger.formatNumber(time.getMinutes())}:${Logger.formatNumber(time.getSeconds())}.${minutesString}`;

    return `[${this.loggerName}][${type}] - ${dateString} ${timeString} - ${message}`;
  }

  private static formatNumber(val: number):string {
    return val < 10 ? '0' + val : '' + val;
  }
}
